package momo.app.config;

import java.net.InetAddress;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import momo.app.structure.Peer;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;


public class AppConfig
{
	private Log log = LogFactory.getLog(AppConfig.class);
	static private AppConfig config;
	private Peer me;
	
	private AppConfig()
	{
	}
	
	static public AppConfig getInstance()
	{
		if ( config == null )
			config = new AppConfig();
		return config;
	}
	
	public void setMe(Peer me)
	{
		if ( me == null )
		{
			if ( log.isDebugEnabled() )
				log.debug("Me is null in setMe(Peer)");
			throw new NullPointerException("Peer me is null");
		}
		this.me = me;
	}
	
	public void setMe(List<InetAddress> listIP, int commPort, int statusPort, double workloadRate, double stability, double cost)
	{
		me = new Peer(listIP, commPort, statusPort, workloadRate, stability, cost);
	}
	
	public Peer getMe()
	{
		if ( me == null )
		{
			if ( log.isDebugEnabled() )
				log.debug("Me is null in getMe()");
			throw new NullPointerException("Peer me is null");
		}
		return me;
	}
	
	public int getStatusPort()
	{
		if ( me == null )
		{
			if ( log.isDebugEnabled() )
				log.debug("Me is null in getStatusPort()");
			throw new NullPointerException("Peer me is null");
		}
		
		return me.getStatusPort(); 
	}
	
	public int getCommPort()
	{
		if ( me == null )
		{
			if ( log.isDebugEnabled() )
				log.debug("Me is null in getCommPort()");
			throw new NullPointerException("Peer me is null");
		}
		
		return me.getCommPort();
	}
	
	public double getCost()
	{
		if ( me == null )
		{
			if ( log.isDebugEnabled() )
				log.debug("Me is null in getCost()");
			throw new NullPointerException("Peer me is null");
		}
		
		return me.getCost();
	}
	
	public void setUserCost(String source, double cost)
	{
		if ( me == null )
		{
			if ( log.isDebugEnabled() )
				log.debug("Me is null in setUserCost(String, double)");
			throw new NullPointerException("Peer me is null");
		}
		
		me.setUserCost(source, cost);
	}
	
	public double getUserCost(String source)
	{
		if ( me == null )
		{
			if ( log.isDebugEnabled() )
				log.debug("Me is null in getUserCost(String)");
			throw new NullPointerException("Peer me is null");
		}
		
		return me.getUserCost(source);
	}
	
	public String getId()
	{
		if ( me == null )
		{
			if ( log.isDebugEnabled() )
				log.debug("Me is null in getId()");
			throw new NullPointerException("Peer me is null");
		}
		
		return me.getId();
	}
	
	public void setWorkloadRate(double workloadRate)
	{
		if ( me == null )
		{
			if ( log.isDebugEnabled() )
				log.debug("Me is null in setWorkloadRate(double)");
			throw new NullPointerException("Peer me is null");
		}
		
		me.setWorkloadRate(workloadRate);
	}
	
	public void setWorkload(int workload)
	{
		if ( me == null )
		{
			if ( log.isDebugEnabled() )
				log.debug("Me is null in setWorkloadRate(int)");
			throw new NullPointerException("Peer me is null");
		}
		me.setWorkload(workload);
	}
	
}//end of class AppConfig
